package dobest.ray.com.jiongjiong.activity.login.dataInport.questionair;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.dobest.ray.corelibs.utils.ToastMgr;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import dobest.ray.com.jiongjiong.R;
import dobest.ray.com.jiongjiong.activity.BaseActivity;
import dobest.ray.com.jiongjiong.activity.MainActivity;
import dobest.ray.com.jiongjiong.authority.AuthorityContext;
import dobest.ray.com.jiongjiong.authority.LoggedIn;
import dobest.ray.com.jiongjiong.bean.QuestionairCallBackBean;
import dobest.ray.com.jiongjiong.bean.QuestionnairBean;
import dobest.ray.com.jiongjiong.callback.A2FCallBack;
import dobest.ray.com.jiongjiong.views.NavigationBar;
import dobest.ray.com.jiongjiong.views.NoScrollViewPager;

public class QuestionnaireSurveyActivity extends BaseActivity implements
        OnClickListener, A2FCallBack<QuestionairCallBackBean> {
    private TextView tvTabActivity, tvTabGroups, tvTabFriends;
    private ImageView ivBottomLine;
    private int bottomLineWidth;
    private int offset = 0;
    private int position_one;
    private int position_two;
    private int currIndex = 0;
    private Resources resources;
    private ArrayList<Fragment> fragmentsList;

    private View view_1;
    private View view_2;
    private View view_3;
    private View view_4;
    private int current = 0;// 当前到第几个关卡
    private ImageView iv_isDone2;
    private ImageView iv_isDone3;

    private NoScrollViewPager mPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_questionnaire_survey);
        findViews();
        init();
        addListeners();
    }

    @Override
    public void findViews() {
        navi = (NavigationBar) findViewById(R.id.navi);
        navi.setTitle("问卷调查");
        navi.setBackListener(R.mipmap.img_back, "");
        navi.setTextRightColor(getResources().getColor(R.color.white));
        view_1 = findViewById(R.id.view_1);
        view_2 = findViewById(R.id.view_2);
        view_3 = findViewById(R.id.view_3);
        view_4 = findViewById(R.id.view_4);
        iv_isDone2 = (ImageView) findViewById(R.id.iv_isDone2);
        iv_isDone3 = (ImageView) findViewById(R.id.iv_isDone3);
    }

    @Override
    public void init() {
        resources = getResources();
        InitWidth();
        InitTextView();
        InitViewPager();
    }

    @Override
    public void addListeners() {

    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_text_right:
                Intent it = new Intent(QuestionnaireSurveyActivity.this, MainActivity.class);
                startActivity(it);
                break;

            default:
                break;
        }

    }

    private void InitWidth() {
        ivBottomLine = (ImageView) findViewById(R.id.iv_bottom_line);
        bottomLineWidth = ivBottomLine.getLayoutParams().width;

        LayoutParams lp = (LayoutParams) ivBottomLine
                .getLayoutParams();

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;
        offset = (int) ((screenW / 3.0 - bottomLineWidth) / 2);
        lp.leftMargin = offset;
        ivBottomLine.setLayoutParams(lp);
        position_one = (int) (screenW / 3.0);
        position_two = position_one * 2;
    }

    private void InitTextView() {
        tvTabActivity = (TextView) findViewById(R.id.tv_tab_activity);
        tvTabGroups = (TextView) findViewById(R.id.tv_tab_groups);
        tvTabFriends = (TextView) findViewById(R.id.tv_tab_friends);

        tvTabActivity.setOnClickListener(new MyOnClickListener(0));
        tvTabGroups.setOnClickListener(new MyOnClickListener(1));
        tvTabFriends.setOnClickListener(new MyOnClickListener(2));
    }

    @Override
    public void getActivityData(QuestionairCallBackBean bean) {
        if (bean != null) {
            if ("confirm_survey".equals(bean.tag)) {
                data.addAll(Questionnair1.getData());
                data.addAll(Questionnair2.getData());
                data.addAll(Questionnair3.getData());
                StringBuilder sb = new StringBuilder();
                for (QuestionnairBean d : data) {

                    sb.append(d.id).append(",").append(d.scale).append(";");
                }
                if (sb.length() > 0) {
                    String itemStr = sb.substring(0, sb.length() - 1);
                    Log.i("wanglei", "itemStr:" + itemStr);

                    AuthorityContext.getContext().setAuthority(new LoggedIn());

                    Intent it = new Intent(QuestionnaireSurveyActivity.this, MainActivity.class);
                    startActivity(it);
                }
            }
            if ("survey_current".equals(bean.tag)) {
                if (bean.index > current) {
                    if (current >= bean.index) {
                        return;
                    }
                    current = bean.index;
                    if (current == 1) {
                        view_1.setBackgroundColor(getResources().getColor(R.color.top_bg));
                        view_2.setBackgroundColor(getResources().getColor(R.color.top_bg));
                        iv_isDone2.setImageResource(R.mipmap.numberseekbar_thumb);
                    }
                    if (current == 2) {
                        view_3.setBackgroundColor(getResources().getColor(R.color.top_bg));
                        view_4.setBackgroundColor(getResources().getColor(R.color.top_bg));
                        iv_isDone3.setImageResource(R.mipmap.numberseekbar_thumb);
                    }
                }
            }
        }
    }

    public class MyOnClickListener implements OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            if (current == 0 && index >= 1) {
                ToastMgr.show("请先完成用眼环境问卷调查");
                return;
            } else if (current == 1 && index > 1) {
                ToastMgr.show("请先完成用眼习惯问卷调查");
                return;
            }
            mPager.setCurrentItem(index);
        }
    }

    ;

    private QuestionnairFragment Questionnair1;
    private QuestionnairFragment Questionnair2;
    private QuestionnairFragment Questionnair3;

    private void InitViewPager() {
        mPager = (NoScrollViewPager) findViewById(R.id.vPager);
        mPager.setOffscreenPageLimit(3);
        fragmentsList = new ArrayList<Fragment>();

        Questionnair1 = new QuestionnairFragment();
        Bundle b1 = new Bundle();
        b1.putInt("index", 1);
        Questionnair1.setArguments(b1);
        Questionnair2 = new QuestionnairFragment();
        Bundle b2 = new Bundle();
        b2.putInt("index", 2);
        Questionnair2.setArguments(b2);
        Questionnair3 = new QuestionnairFragment();
        Bundle b3 = new Bundle();
        b3.putInt("index", 3);
        Questionnair3.setArguments(b3);

        fragmentsList.add(Questionnair1);
        fragmentsList.add(Questionnair2);
        fragmentsList.add(Questionnair3);

        mPager.setAdapter(new QuestionnairPagerAdapter(this
                .getSupportFragmentManager(), fragmentsList));
        mPager.setCurrentItem(0);
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }

    private List<QuestionnairBean> data = new ArrayList<QuestionnairBean>();


    public class MyOnPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
            Animation animation = null;
            switch (arg0) {
                case 0:
                    if (currIndex == 1) {
                        animation = new TranslateAnimation(position_one, 0, 0, 0);
                        tvTabGroups
                                .setTextColor(resources.getColor(R.color.text_gray));
                    } else if (currIndex == 2) {
                        animation = new TranslateAnimation(position_two, 0, 0, 0);
                        tvTabFriends.setTextColor(resources
                                .getColor(R.color.text_gray));
                    }
                    tvTabActivity.setTextColor(resources.getColor(R.color.top_bg));
                    currIndex = 0;
                    break;
                case 1:
                    if (current < 1) {
                        mPager.setCurrentItem(0);
                        ToastMgr.show("请先完成用眼环境问卷调查");
                        return;
                    }
                    if (currIndex == 0) {
                        animation = new TranslateAnimation(offset, position_one, 0,
                                0);
                        tvTabActivity.setTextColor(resources
                                .getColor(R.color.text_gray));
                    } else if (currIndex == 2) {
                        animation = new TranslateAnimation(position_two,
                                position_one, 0, 0);
                        tvTabFriends.setTextColor(resources
                                .getColor(R.color.text_gray));
                    }
                    tvTabGroups.setTextColor(resources.getColor(R.color.top_bg));
                    currIndex = 1;
                    break;
                case 2:
                    if (current == 0) {
                        mPager.setCurrentItem(0);
                        ToastMgr.show("请先完成用眼环境问卷调查");
                        return;
                    } else if (current == 1) {
                        mPager.setCurrentItem(1);
                        ToastMgr.show("请先完成用眼习惯问卷调查");
                        return;
                    }
                    if (currIndex == 0) {
                        animation = new TranslateAnimation(offset, position_two, 0,
                                0);
                        tvTabActivity.setTextColor(resources
                                .getColor(R.color.text_gray));
                    } else if (currIndex == 1) {
                        animation = new TranslateAnimation(position_one,
                                position_two, 0, 0);
                        tvTabGroups
                                .setTextColor(resources.getColor(R.color.text_gray));
                    }
                    tvTabFriends.setTextColor(resources.getColor(R.color.top_bg));
                    currIndex = 2;

                    break;
            }

            if (animation != null) {
                animation.setFillAfter(true);
                animation.setDuration(300);
                ivBottomLine.startAnimation(animation);
            }

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }

}
